package client.popups.meeting;

import java.awt.Component;
import java.util.ArrayList;

import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import miscellaneous.Storage;

import data.Invite;
import data.Meeting;
import data.Message;
import data.User;

public class ParticipantRenderer extends DefaultListCellRenderer implements ListCellRenderer{
	
	private ArrayList<Message> messages = Storage.messages.getCopyOfList();
	
	private Meeting meeting;
	
	public ParticipantRenderer(Meeting meeting) {
		super();
		
		this.meeting = meeting;
	}
	
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		
		JLabel label = null;
		
		if(value == null)
			label = new JLabel();
		else 
			label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
		
		User participant = (User)value;
		for (Message m : messages) {
			if(m instanceof Invite && ((Invite)m).getReceiver() == participant && meeting == ((Invite)m).getMeeting()){
				Invite i = (Invite) m;
				if(i.getAnswer() == Invite.ANSWER_NO) label.setIcon(new ImageIcon(("resources/icons/101.png")));
				else if(i.getAnswer() == Invite.ANSWER_YES) label.setIcon(new ImageIcon("resources/icons/102.png"));
				else label.setIcon(new ImageIcon(("resources/icons/103.png")));
				
				label.setText(participant.getName() + " <" + participant.getEmail() + "> ");
				return label;
			}
		}
		label.setText(participant.getName() + " <" + participant.getEmail() + "> unrendered");
		return label;
	}
}
